package com.inhim.pjhim.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inhim.pjhim.R;
import com.inhim.pjhim.adapter.HelpListAdapter;
import com.inhim.pjhim.app.BaseActivity;
import com.inhim.pjhim.entity.CommonDictEntity;
import com.inhim.pjhim.entity.HelpListEntity;
import com.inhim.pjhim.http.MyOkHttpClient;
import com.inhim.pjhim.http.Urls;
import com.inhim.pjhim.view.BToast;

import org.json.JSONException;
import org.json.JSONObject;
import org.yczbj.ycrefreshviewlib.view.YCRefreshView;

import java.io.IOException;

import okhttp3.Request;

public class IssueListActivity extends BaseActivity {
    private Gson gson = new Gson();
    private int mPageNum=1;
    private Boolean refresh=true;
    private YCRefreshView ycRefreshView;
    private HelpListAdapter mAdapter;
    @Override
    public Object offerLayout() {
        return R.layout.activity_issue_list;
    }

    @Override
    public void onBindView() {
        ImageView iv_back=findViewById(R.id.iv_back);
        iv_back.setOnClickListener(v -> finish());
        TextView iv_title=findViewById(R.id.tvCourse);
        iv_title.setText("常见问题");
        ycRefreshView=findViewById(R.id.ycRefreshView);
        initAdavter();
        getCommonDict();
    }

    private void initAdavter() {
        mAdapter=new HelpListAdapter(IssueListActivity.this);
        ycRefreshView.setLayoutManager(new LinearLayoutManager(IssueListActivity.this));
        ycRefreshView.setAdapter(mAdapter);
        mAdapter.setMore(R.layout.view_more, () -> {
            //可以做请求下一页操作
            mPageNum++;
            refresh=false;
            getCommonDict();
        });
        ycRefreshView.setRefreshListener(() -> {
            mPageNum= 1;
            refresh=true;
            getCommonDict();
        });
    }

    @Override
    public void destory() {

    }

    private void getCommonDict() {
        showLoading("加载中");
        MyOkHttpClient.getInstance().asyncGet(Urls.getHelp(mPageNum,10), new MyOkHttpClient.HttpCallBack() {
            @Override
            public void onError(Request request, IOException e) {
                hideLoading();
                BToast.showText("请求失败", false);
            }

            @Override
            public void onSuccess(Request request, String result) {
                hideLoading();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.getInt("code") == 0) {
                        HelpListEntity helpListEntity = gson.fromJson(result, HelpListEntity.class);
                        if(refresh){
                            mAdapter.clear();
                        }
                        mAdapter.addAll(helpListEntity.getPage().getList());
                        mAdapter.notifyDataSetChanged();
                    } else {
                        BToast.showText(jsonObject.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}
